package com.cx.controller.wjr;

import com.cx.pojo.User;
import com.cx.pojo.UserExample;
import com.cx.service.wjr.UserService;
import com.cx.vo.ResponseResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;

@Controller
@RequestMapping("admin")
public class AdminUserController {
    @Autowired
    UserService userService;
    @RequestMapping("addUser")
    @ResponseBody
    public ResponseResult add(ResponseResult responseResult, Model model, User user){
        int userNum = 0;
        try{
            userNum =  userService.addCourier(user);

        }catch (Exception e){
            e.printStackTrace();
        }
        System.out.println(userNum);
        if(userNum > 0){
            responseResult.setCode(0);
            responseResult.setMessage("添加成功");
            return responseResult;
        }else{
            responseResult.setCode(1);
            responseResult.setMessage("添加失败");
            return responseResult;
        }
    }

    @RequestMapping("adminUserList")
    @ResponseBody
    public ResponseResult list(Integer pageNum,ResponseResult responseResult,Model model, UserExample userExample){
        PageHelper.startPage(pageNum, 5);
        List<User> userList =  userService.selectByExample(userExample);
        PageInfo pageInfo = new PageInfo(userList,5);
        userList.forEach(System.out::println);
        responseResult.setData(pageInfo);
        return responseResult;
    }

    @RequestMapping("adminUpdateUser")
    @ResponseBody
    public ResponseResult update(ResponseResult responseResult,Model model,User user, UserExample userExample){
        if(null != user.getAddress() && user.getAddress() != ""){
            userExample.createCriteria().andAddressEqualTo(user.getAddress());
        }
        if(null != user.getAge()){
            userExample.createCriteria().andAgeEqualTo(user.getAge());
        }
        if(null != user.getSex() && user.getSex() != ""){
            userExample.createCriteria().andSexEqualTo(user.getSex());
        }
        if(null != user.getIdCard()){
            userExample.createCriteria().andIdCardEqualTo(user.getIdCard());
        }
        if(null != user.getUserName() && user.getUserName() != ""){
            userExample.createCriteria().andUserNameEqualTo(user.getUserName());
        }
        if(null != user.getUserPhone() && user.getUserPhone() != ""){
            userExample.createCriteria().andUserPhoneEqualTo(user.getUserPhone());
        }
        int courierList =  userService.update(user,userExample);
        //courierList.forEach(System.out::println);
        responseResult.setData(courierList);
        return responseResult;
    }

    @RequestMapping("adminSelectUser")
    @ResponseBody
    public ResponseResult select(ResponseResult responseResult, Model model,User user, UserExample userExample){
//        userExample.createCriteria().andUserPhoneEqualTo("1235");
        if(null != user.getUserPhone() && user.getUserPhone() != ""){
            userExample.createCriteria().andUserPhoneEqualTo(user.getUserPhone());
        }
        List<User> userNum = userService.selectByExample(userExample);
        userNum.forEach(System.out::println);
        System.out.println(userNum);
        responseResult.setData(userNum);
        if(userNum.size() > 0){
            responseResult.setMessage("查询成功");
            return responseResult;
        }
        responseResult.setMessage("查询失败");
        return responseResult;
    }

    @RequestMapping("adminUserDelete")
    @ResponseBody
    public ResponseResult userDelete(ResponseResult responseResult,UserExample userExample,User user , Model model){
        if(null != user){
            int count =  userService.delete(userExample);
            if(count>0){
                responseResult.setMessage("删除成功");
                return responseResult;
            }else{
                responseResult.setMessage("删除失败");
                return responseResult;
            }
        }
        return responseResult;
    }

}
